package com.maxmlr.music.song.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.maxmlr.music.admin.service.UserService;
import com.maxmlr.music.common.base.BaseServiceImpl;
import com.maxmlr.music.common.exception.XmlrException;
import com.maxmlr.music.song.dao.SingerMapper;
import com.maxmlr.music.song.dao.SingerProfilesMapper;
import com.maxmlr.music.song.entity.Singer;
import com.maxmlr.music.song.entity.SingerProfiles;
import com.maxmlr.music.song.param.SingerInfoParam;
import com.maxmlr.music.song.service.SingerService;
import com.maxmlr.music.song.service.transfer.SingerTransfer;
import com.maxmlr.music.system.service.FilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author max
 * @version 1.0
 * @ClassName SingerServiceImpl
 * @Description 服务实现类
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-07
 */
@Slf4j
@Service
public class SingerServiceImpl extends BaseServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    private SingerProfilesMapper singerProfilesMapper;

    @Autowired
    private FilesService filesService;

    @Autowired
    private UserService userService;

    @Autowired
    private SingerTransfer singerTransfer;

    @Override
    public SingerInfoParam getSingerInfoById(Integer id) {
        Singer singer = getById(id);
        SingerProfiles singerProfiles = singerProfilesMapper.selectOne(Wrappers.<SingerProfiles>lambdaQuery()
                .eq(SingerProfiles::getSingerId, id));

        SingerInfoParam singerInfoParam = singerTransfer.po2SingerInfoParam(singer, singerProfiles);
        filesService.getFileById(singerInfoParam, SingerInfoParam::getHeadImgId, SingerInfoParam::setHeadImg);
        return singerInfoParam;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveParam(SingerInfoParam param) {
        Singer one = getOne(Wrappers.<Singer>lambdaQuery().eq(Singer::getSingerName, param.getSingerName()));
        if (Objects.nonNull(one)) {
            throw new XmlrException("歌手名重复");
        }
        Singer singer = singerTransfer.po2Singer(param, userService.getAdminUser());
        save(singer);
        SingerProfiles singerProfiles = singerTransfer.po2SingerProfiles(singer, param);
        singerProfilesMapper.insert(singerProfiles);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateParamById(SingerInfoParam param) {
        Singer singer = getById(param.getId());
        if (Objects.isNull(singer)) {
            throw new XmlrException("歌手信息不存在");
        }
        SingerProfiles singerProfiles = singerProfilesMapper.selectOne(Wrappers.<SingerProfiles>lambdaQuery()
                .eq(SingerProfiles::getSingerId, singer.getId()));
        singerTransfer.fullSinger(singer, param, userService.getAdminUser());
        singerTransfer.fullSingerProfiles(singerProfiles, param, singer);
        updateById(singer);
        singerProfilesMapper.updateById(singerProfiles);
        return false;
    }

    @Override
    public List<SingerInfoParam> transferParams(List<Singer> list) {
        List<SingerInfoParam> params = list.stream().map(singerTransfer::po2SingerInfoParam).collect(Collectors.toList());
        filesService.getFilesByIds(params, SingerInfoParam::getHeadImgId, SingerInfoParam::setHeadImg);
        return params;
    }
}
